import { createRouter, createWebHistory} from 'vue-router'
import Home from '../view/home/index.vue'
import { showFailToast } from 'vant'
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {is_footer: true, is_header: true}
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('../view/list/index.vue'),
    meta: {is_footer: false, is_header: true}
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../view/cart/index.vue'),
    meta: {is_footer: true, is_header: true}
  },
  {
    path: '/self',
    name: 'self',
    component: () => import('../view/self/index.vue'),
    meta: {is_footer: false, is_header: false}
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('../view/detail/index.vue'),
    meta: {is_footer: false, is_header: false}
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../view/search/index.vue'),
    meta: {is_footer: false, is_header: true}
  },
  {
    path: '/cate',
    name: 'cate',
    component: () => import('../view/cate/index.vue'),
    meta: {is_footer: true, is_header: true}
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../view/login/index.vue'),
    meta: {is_footer: false, is_header: true}
  },                                                                      
  {
    path: '/register',
    name: 'register',
    component: () => import('../view/register/index.vue'),
    meta: {is_footer: false, is_header: true}
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

const list: Array<string> = ['/self']

//  路由首位验证是否登录
router.beforeEach( to => {

  if ( list.indexOf(to.fullPath) === -1) return true
 
  const token:string = window.localStorage.getItem('token') || ''

  if (token) return true

  showFailToast('您没有登录')

  router.push('/login')

  return false
}) 

export default router
